package com.srt.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.srt.model.Catalog;
import com.srt.model.SrtContent;
import com.srt.service.CatalogService;
import com.srt.service.SrtContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author rd06
 * @since 2021-03-29
 */
@Controller
@RequestMapping("/srtContent")
public class SrtContentController {
    @Autowired
    SrtContentService srtContentService;
    @Autowired
    private CatalogService catalogService;

    @GetMapping("/list/{catalogId}")
    public String list(HttpServletRequest request, @PathVariable("catalogId") Long cataLogId) {
        List<SrtContent> srtContentList = srtContentService.getSrtContextList(cataLogId);
        request.setAttribute("srtContentList", srtContentList);
        return "srt_list";
    }

    @PostMapping("/upload")
    public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file, @RequestParam("catalogName") String catalogName,
                         @RequestParam("parentId") Long parentId, @RequestParam("type") int type) {
        Catalog catalog = Catalog.builder().name(catalogName).fileName(file.getOriginalFilename())
                .parentId(parentId == null ? 0L : parentId).type(type).isDelete(0).build();
        catalogService.save(catalog);
        if (type == 2) {
            srtContentService.uploadSrt(file, catalog.getId());
        }
        List<SrtContent> srtContentList = srtContentService.getSrtContextList(catalog.getId());
        request.setAttribute("srtContentList", srtContentList);
        return "srt_list";
    }


    @PutMapping("/edit/{catalogId}")
    @ResponseBody
    public Boolean edit(@PathVariable("catalogId") Long cataLogId, @RequestBody List<SrtContent> srtContentList, HttpServletRequest request) {
        srtContentService.updateBatchSrtContent(srtContentList);
        return true;
    }

    @GetMapping("/export/{catalogId}")
    public void export(@PathVariable("catalogId") Long cataLogId, HttpServletResponse response) throws IOException {
        Catalog catalog = catalogService.getById(cataLogId);
        List<SrtContent> srtContentList = srtContentService.getSrtContextList(cataLogId);

        System.out.println(JSON.toJSONString(srtContentList));

        StringBuilder stringBuilder = new StringBuilder();
        srtContentList.forEach(item -> {
            stringBuilder.append(item.getOrderNum());
            stringBuilder.append("\r\n");
            stringBuilder.append(item.getStartTime() + " --> " + item.getEndTime());
            stringBuilder.append("\r\n");
            if (StrUtil.isNotEmpty(item.getChContent())) {
                stringBuilder.append(item.getChContent());
                stringBuilder.append("\r\n");
            }
            if (StrUtil.isNotEmpty(item.getEnContent())) {
                stringBuilder.append(item.getEnContent());
                stringBuilder.append("\r\n");
            }
            stringBuilder.append("\r\n");
        });
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(catalog.getFileName(), "UTF-8") + ".srt");// 设置文件名
        response.getOutputStream().write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
    }

}

